package com.xing.gulimall.product.service.impl;

import com.xing.gulimall.common.utils.PageUtils;
import com.xing.gulimall.common.utils.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import com.xing.gulimall.product.dao.CategoryDao;
import com.xing.gulimall.product.entity.CategoryEntity;
import com.xing.gulimall.product.service.CategoryService;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> listWithTree() {
//        //查出所有商品菜单
//        List<CategoryEntity> entities = baseMapper.selectList(null);
//        //以树形组装父子关系
//        List<CategoryEntity> level1Menus = entities.stream()
//                .filter(categoryEntity -> categoryEntity.getParentCid() == 0
//                )
//                .map(categoryEntity -> {
//                    categoryEntity.setChildren(getChild(categoryEntity, entities));
//                    return categoryEntity;
//                })
//                .sorted((menu1, menu2) ->{
//                            return (menu1.getSort() == null ? 0 : menu1.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort());
//                        }
//                )
//                .collect(Collectors.toList());
//
//        return level1Menus;


        //查出所有分类
        //组装成父子树形结构
        //此处的baseMapper就是categoryDao
        List<CategoryEntity> entities= baseMapper.selectList(null);
        //传入断言
        List<CategoryEntity> leve1Menus = entities.stream()
                .filter(categoryEntity-> categoryEntity.getParentCid() == 0)
                .map(menu -> {
                    menu.setChildren(getChildrens(menu, entities));
                    return menu;
                })
                .sorted((menu1, menu2) -> {
                    return (menu1.getSort() == null ? 0 : menu1.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort());
                }).collect(Collectors.toList());
        return leve1Menus;
    }

    //获取当前菜单的所有子菜单以及子菜单的子菜单，相当于添加categoryEntity字段的child属性。
    //一个菜单的子菜单的特点是，子菜单的parentCid = 父菜单的catId
    public List<CategoryEntity> getChildrens(CategoryEntity root, List<CategoryEntity> all){
//       List<CategoryEntity> child = all.stream()
//               .filter(categoryEntity  ->{
//                   return categoryEntity.getParentCid() == root.getCatId();
//               })
//               .map(categoryEntity -> {
//                   categoryEntity.setChildren(getChild(categoryEntity, all));
//                   return categoryEntity;
//               })
//               .sorted((menu1, menu2) ->{
//                   return (menu1.getSort() == null ? 0 : menu1.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort());
//               })
//               .collect(Collectors.toList());
//        return child;


        List<CategoryEntity> child = all.stream()
                //找到root的子菜单
                .filter(categoryEntity ->{
                    return categoryEntity.getParentCid() == root.getCatId();
                    //为root的子菜单设置child，相当于为子菜单设置子菜单
                }).map(categoryEntity -> {
                    categoryEntity.setChildren(getChildrens(categoryEntity, all));
                    return categoryEntity;
                    //菜单排序
                }).sorted((menu1, menu2) ->{
                    return (menu1.getSort() == null ? 0 : menu1.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort());
                }).collect(Collectors.toList());
        return child;
    }
}